package com.jvtd.jpush;

import android.content.Context;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;

public class JvtdPush
{
  private static volatile JvtdPush instance;
  private Context mContext;

  private JvtdPush(Context context)
  {
    mContext = context;
  }

  public static JvtdPush getInstance(Context context)
  {
    if (instance == null)
      synchronized (JvtdPush.class)
      {
        if (instance == null)
          instance = new JvtdPush(context.getApplicationContext());
      }
    return instance;
  }

  /**
   * 初始化
   *
   * @author Chenlei
   * created at 2018/10/9
   **/
  public JvtdPush init()
  {
    JPushInterface.init(mContext);
    return this;
  }

  /**
   * 开发模式 正式发布注释
   *
   * @author Chenlei
   * created at 2018/10/9
   **/
  public JvtdPush isDevelopment()
  {
    JPushInterface.setDebugMode(true);
    return this;
  }

  /**
   * 设置别名
   * 注：这个接口是覆盖逻辑，而不是增量逻辑。即新的调用会覆盖之前的设置。
   *
   * @author Jack Zhang
   * create at 2018/12/26 5:09 PM
   */
  public JvtdPush setAlias(String alias)
  {
    if (!JvtdPushUtils.isEmpty(alias) && JvtdPushUtils.isValidTagAndAlias(alias))
    {
      JvtdPushTagAliasHelper.TagAliasBean tagAliasBean = new JvtdPushTagAliasHelper.TagAliasBean();
      tagAliasBean.action = JvtdPushTagAliasHelper.ACTION_SET;
      tagAliasBean.alias = alias;
      tagAliasBean.isAliasAction = true;
      JvtdPushTagAliasHelper.sequence++;
      JvtdPushTagAliasHelper.getInstance().handleAction(mContext, JvtdPushTagAliasHelper.sequence, tagAliasBean);
    }
    return this;
  }

  /**
   * 删除别名
   *
   * @author Jack Zhang
   * create at 2018/12/26 5:10 PM
   */
  public JvtdPush delAlias()
  {
    JvtdPushTagAliasHelper.TagAliasBean tagAliasBean = new JvtdPushTagAliasHelper.TagAliasBean();
    tagAliasBean.action = JvtdPushTagAliasHelper.ACTION_DELETE;
    tagAliasBean.isAliasAction = true;
    JvtdPushTagAliasHelper.sequence++;
    JvtdPushTagAliasHelper.getInstance().handleAction(mContext, JvtdPushTagAliasHelper.sequence, tagAliasBean);
    return this;
  }

  /**
   * 查询别名
   *
   * @author Jack Zhang
   * create at 2018/12/26 5:11 PM
   */
  public JvtdPush getAlias()
  {
    JvtdPushTagAliasHelper.TagAliasBean tagAliasBean = new JvtdPushTagAliasHelper.TagAliasBean();
    tagAliasBean.action = JvtdPushTagAliasHelper.ACTION_GET;
    tagAliasBean.isAliasAction = true;
    JvtdPushTagAliasHelper.sequence++;
    JvtdPushTagAliasHelper.getInstance().handleAction(mContext, JvtdPushTagAliasHelper.sequence, tagAliasBean);
    return this;
  }

  /**
   * 设置标签
   * 注：这个接口是覆盖逻辑，而不是增量逻辑。即新的调用会覆盖之前的设置。
   *
   * @author Jack Zhang
   * create at 2018/12/26 5:07 PM
   */
  public JvtdPush setTags(Set<String> tags)
  {
    if (tags == null) return this;
    boolean containsBadTag = false;
    for (String tag : tags)
      if (JvtdPushUtils.isEmpty(tag) || !JvtdPushUtils.isValidTagAndAlias(tag))
      {
        containsBadTag = true;
        break;
      }
    if (containsBadTag) return this;

    JvtdPushTagAliasHelper.TagAliasBean tagAliasBean = new JvtdPushTagAliasHelper.TagAliasBean();
    tagAliasBean.action = JvtdPushTagAliasHelper.ACTION_SET;
    tagAliasBean.tags = tags;
    tagAliasBean.isAliasAction = false;
    JvtdPushTagAliasHelper.sequence++;
    JvtdPushTagAliasHelper.getInstance().handleAction(mContext, JvtdPushTagAliasHelper.sequence, tagAliasBean);
    return this;
  }

  /**
   * 新增标签
   * 注：每次调用至少新增一个 tag
   *
   * @author Jack Zhang
   * create at 2018/12/26 5:07 PM
   */
  public JvtdPush addTags(Set<String> tags)
  {
    if (tags == null) return this;
    boolean containsBadTag = false;
    for (String tag : tags)
      if (JvtdPushUtils.isEmpty(tag) || !JvtdPushUtils.isValidTagAndAlias(tag))
      {
        containsBadTag = true;
        break;
      }
    if (containsBadTag) return this;

    JvtdPushTagAliasHelper.TagAliasBean tagAliasBean = new JvtdPushTagAliasHelper.TagAliasBean();
    tagAliasBean.action = JvtdPushTagAliasHelper.ACTION_ADD;
    tagAliasBean.tags = tags;
    tagAliasBean.isAliasAction = false;
    JvtdPushTagAliasHelper.sequence++;
    JvtdPushTagAliasHelper.getInstance().handleAction(mContext, JvtdPushTagAliasHelper.sequence, tagAliasBean);
    return this;
  }

  /**
   * 删除指定标签
   * 注：每次调用至少删除一个 tag。
   *
   * @author Jack Zhang
   * create at 2018/12/26 5:07 PM
   */
  public JvtdPush deleteTags(Set<String> tags)
  {
    if (tags == null) return this;
    boolean containsBadTag = false;
    for (String tag : tags)
      if (JvtdPushUtils.isEmpty(tag) || !JvtdPushUtils.isValidTagAndAlias(tag))
      {
        containsBadTag = true;
        break;
      }
    if (containsBadTag) return this;

    JvtdPushTagAliasHelper.TagAliasBean tagAliasBean = new JvtdPushTagAliasHelper.TagAliasBean();
    tagAliasBean.action = JvtdPushTagAliasHelper.ACTION_DELETE;
    tagAliasBean.tags = tags;
    tagAliasBean.isAliasAction = false;
    JvtdPushTagAliasHelper.sequence++;
    JvtdPushTagAliasHelper.getInstance().handleAction(mContext, JvtdPushTagAliasHelper.sequence, tagAliasBean);
    return this;
  }

  /**
   * 清除所有标签
   *
   * @author Jack Zhang
   * create at 2018/12/26 5:07 PM
   */
  public JvtdPush cleanTags()
  {
    JvtdPushTagAliasHelper.TagAliasBean tagAliasBean = new JvtdPushTagAliasHelper.TagAliasBean();
    tagAliasBean.action = JvtdPushTagAliasHelper.ACTION_CLEAN;
    tagAliasBean.isAliasAction = false;
    JvtdPushTagAliasHelper.sequence++;
    JvtdPushTagAliasHelper.getInstance().handleAction(mContext, JvtdPushTagAliasHelper.sequence, tagAliasBean);
    return this;
  }

  /**
   * 查询所有标签
   *
   * @author Jack Zhang
   * create at 2018/12/26 5:07 PM
   */
  public JvtdPush getAllTags()
  {
    JvtdPushTagAliasHelper.TagAliasBean tagAliasBean = new JvtdPushTagAliasHelper.TagAliasBean();
    tagAliasBean.action = JvtdPushTagAliasHelper.ACTION_GET;
    tagAliasBean.isAliasAction = false;
    JvtdPushTagAliasHelper.sequence++;
    JvtdPushTagAliasHelper.getInstance().handleAction(mContext, JvtdPushTagAliasHelper.sequence, tagAliasBean);
    return this;
  }

  /**
   * 查询指定 tag 与当前用户绑定的状态
   * 注：传的参数是Set，但是一次只能check一个tag
   *
   * @author Jack Zhang
   * create at 2018/12/26 5:07 PM
   */
  public JvtdPush checkTagBindState(Set<String> tags)
  {
    if (tags == null) return this;
    boolean containsBadTag = false;
    for (String tag : tags)
      if (JvtdPushUtils.isEmpty(tag) || !JvtdPushUtils.isValidTagAndAlias(tag))
      {
        containsBadTag = true;
        break;
      }
    if (containsBadTag) return this;

    JvtdPushTagAliasHelper.TagAliasBean tagAliasBean = new JvtdPushTagAliasHelper.TagAliasBean();
    tagAliasBean.action = JvtdPushTagAliasHelper.ACTION_CHECK;
    tagAliasBean.tags = tags;
    tagAliasBean.isAliasAction = false;
    JvtdPushTagAliasHelper.sequence++;
    JvtdPushTagAliasHelper.getInstance().handleAction(mContext, JvtdPushTagAliasHelper.sequence, tagAliasBean);
    return this;
  }
}
